<?php
/**
 * Created by PhpStorm.
 * User: liujialiang
 * Date: 2017/8/21
 * Time: 14:07
 */
include 'functions.php';

function resetArr($arr){
    $newArr=[];
    $value=$arr['name'];
    foreach($value as $k=>$v){
        $newArr[]=[
            'name'=>$v,
            'type'=>$arr['type'][$k],
            'tmp_name'=>$arr['tmp_name'][$k],
            'error'=>$arr['error'][$k],
            'size'=>$arr['size'][$k],
        ];
    }
    return $newArr;
}

function filter($v,$k){
    $k=$k+1;
    $error=false;
    $allowType=['image/jpeg','image/jpg','image/png','image/gif','image/bmp'];
    if(!in_array($v['type'],$allowType)){
        echo '第'.$k.'文件上传失败,文件类型不合法!<br>';
        $error=true;
    }
    if($v['size']<0 || $v['size']>1024*1024*5){
        echo '第'.$k.'文件上传失败,文件大小不合法!<br>';
        $error=true;
    }
    if($v['error']>0){
        echo '文件上传错误!';
        switch($v['error']){
            case 1:
                echo '第'.$k.'文件上传失败,超过配置文件默认大小!<br>';
                $error=true;
            case 2:
                echo '第'.$k.'文件上传失败,超过表单设置大小!<br>';
                $error=true;
            case 3:
                echo '第'.$k.'文件上传失败,部分文件上传失败!<br>';
                $error=true;
            case 4:
                echo '第'.$k.'文件上传失败,没有文件上传!<br>';
                $error=true;
        }
    }
        return $error;
}

if(IS_POST) {
    p($_FILES['up']);
    $count=0;
    $error=false;
    $files=resetArr($_FILES['up']);
    p($files);
    foreach($files as $k=>$v){
        //1.判断是否是可上传文件
        if (!is_uploaded_file($v['tmp_name'])) {
            echo '不是可上传文件!<br>';
            return;
        }

        $error=filter($v,$k);
        if($error){
            continue;
            echo $error;
        }

//2.设置保存文件目录
        $path = './up/' . date('Y-m-d', time()) . '/';
//3.判断是否是一个存在的目录,不存在则创建
        if (!is_dir($path)) {
            mkdir($path, 777, true);
        }
        $v['name'] = htmlspecialchars($v['name']);
//4.1 获取文件扩展名
//        $extends=strrchr($v['name'],'.');
//4.2 获取文件扩展名
        $extends = pathinfo($v['name']);
        $extends = $extends['extension'];
//5  设置文件名
        $file_name = time() . '_' . mt_rand(0, 9999) . $extends;
//6.获取完整路径
        $up_path = $path . $file_name;
//7.上传
        if (move_uploaded_file($v['tmp_name'], $up_path)) {
            $kk=$k+1;
            echo '第'.$kk.'个文件上传成功!<br>';
            $count++;
        } else {
            echo '上传失败!<br>';
        }
    }
    echo '共'.count($files).'个文件,'.$count.'个上传成功!<br>';
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<!--文件上传是表单一定加上enctype="multipart/form-data"属性-->
<form action="" method="post" enctype="multipart/form-data">
    文件:<input type="file" name="up[]" id="" multiple="multiple">
	<br>
	<input type="submit" value="上传">
</form>


</body>
</html>